package com.yzy.util.algorithm;

import org.springframework.util.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class MD5FileUtils {
    public static String getFileMD5(String path) throws IOException {
        FileInputStream fis = new FileInputStream(new File(path));
        String md5 = DigestUtils.md5DigestAsHex(fis);
        fis.close();
        return md5;
    }

    public static String getFileMD5(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        String md5 = DigestUtils.md5DigestAsHex(fis);
        fis.close();
        return md5;
    }

    public static String getStringMD5(String stringValue) {
        return DigestUtils.md5DigestAsHex(stringValue.getBytes(StandardCharsets.UTF_8));
    }

}
